View Modifiers can also be used to modify other View Modifiers.
In this example ShadowStyle will be applied to LabelStyle and ValueStyle View Modifiers.
Since both are supposed to have the same shadow effect, this allows us to control this effect in one place.
ContentView.swift
struct ContentView: View {
struct ShadowStyle: ViewModifier {
func body(content: Content) -> some View {
return content
.shadow(color: Color.black, radius: 2, x: 2, y: 2)
}
}
struct LabelStyle: ViewModifier {
func body(content: Content) -> some View {
return content
.modifier(ShadowStyle())
.foregroundColor(Color.red)
.font(Font.custom("Arial Rounded MT Bold", size: 18))
}
}
struct ValueStyle: ViewModifier {
func body(content: Content) -> some View {
return content
.modifier(ShadowStyle())
.foregroundColor(Color.black)
.font(Font.custom("Arial Rounded MT Bold", size: 24))
}
}
var body: some View {
HStack {
Text("Score:").modifier(LabelStyle())
Text(String("10")).modifier(ValueStyle())
}
}
}
View